package com.ruoyi.controller.system;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.LiveStatus;
import com.ruoyi.common.enums.RuleType;
import com.ruoyi.common.enums.pay.TimeUnit;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysControl;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.SysRole;
import com.ruoyi.system.service.ISysControlService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.vo.SysControlRes;
import com.ruoyi.system.vo.req.SysControlReq;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.ListUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 系统风控信息
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/control")
public class SysControlController extends BaseController
{
    private String prefix = "system/control";

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISysControlService controlService;


    @RequiresPermissions("system:control:view")
    @GetMapping()
    public String view(ModelMap mmap)
    {
        //查询是否存在系统风控
        SysControl search = new SysControl();
        search.setControlType(RuleType.SYSTEM.getCode());
        List<SysControlRes> controlList = controlService.selectSysControlResList(search);
        if (ListUtils.isEmpty(controlList)){
            //新建系统风控
            SysControl sysControl = new SysControl();
            sysControl.setControlType(RuleType.SYSTEM.getCode());
            sysControl.setRelationId(-1L);
            sysControl.setMinBalance(BigDecimal.ZERO);
            sysControl.setMaxBalance(BigDecimal.ZERO);
            sysControl.setTodayBalance(BigDecimal.ZERO);
            String dateStr = "00:00:00";
            sysControl.setStartTime(DateUtil.parse(dateStr));
            sysControl.setEndTime(DateUtil.parse(dateStr));
            sysControl.setControlStatus(LiveStatus.OK.getCode());
            sysControl.setTimeRange(0L);
            sysControl.setTimeUnit(TimeUnit.SECOND.getCode());
            sysControl.setTimeSize(0L);
            sysControl.setTimeBalance(BigDecimal.ZERO);
            controlService.insertSysControl(sysControl);
            mmap.put("sysControl",sysControl);
        }else {
            SysControlRes sysControlRes = controlList.get(0);
//            SysControl sysControl = controlList.get(0);
//            SysControlRes sysControlRes = new SysControlRes();
//            BeanUtils.copyProperties(sysControl,sysControlRes);
            mmap.put("sysControl",sysControlRes);
        }
        return prefix + "/control";
    }

    @RequiresPermissions("system:control:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysControlReq sysControlReq)
    {
        SysControl sysControl = new SysControl();
        BeanUtils.copyProperties(sysControlReq,sysControl);
        sysControl.setStartTime(DateUtil.parse(sysControlReq.getStartTime()));
        sysControl.setEndTime(DateUtil.parse(sysControlReq.getEndTime()));
        return toAjax(controlService.updateSysControl(sysControl));
    }
}
